/**
 * Copyright (c) 2016-2019  All rights reserved.
 * <p>
 *
 * <p>
 * 版权所有，侵权必究！
 */

package io.yuncheng.modules.job.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.yuncheng.common.utils.DateUtils;
import io.yuncheng.modules.job.entity.ScheduleParamEntity;
import io.yuncheng.modules.sys.entity.SubApplicationEntity;
import io.yuncheng.modules.sys.service.SubApplicationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 定时任务接口，所有定时任务都要实现该接口
 * 1.算法汇总(subAppLogArithmeticTask)   -> 汇总算法次数 以及 算法路径
 * 2.导入次数汇总 -> 每个应用对应的数据处理
 *          mark 是否加个字段用以区分是否已经导入id+
 * 3.传递机构以及部门 -> 监管平台
 *
 * 4.传递汇总信息 ->
 *          mark 记录日志
 * @autor jared.shou
 * */
@Service
@Slf4j
abstract class AppTask  implements ITask{
    @Autowired
    private SubApplicationService applicationService;
    public  final  String START = "startDate";
    public  final  String END = "endDate";

    /**
     * 抽象实现
     */
    void execute(ScheduleParamEntity entity ){
        if (ObjectUtils.isEmpty(entity.getAppUniqueIds())) {
            List<SubApplicationEntity> list = applicationService.list();
            List<String> collect = list.stream().map(SubApplicationEntity::getUniqueAppId).collect(Collectors.toList());
            entity.setAppUniqueIds(collect);
            entity.setList(list);
        }
        if (!ObjectUtils.isEmpty(entity.getAppUniqueIds())) {
            QueryWrapper<SubApplicationEntity> appQuery = new QueryWrapper<SubApplicationEntity>().in("unique_app_id", entity.getAppUniqueIds());
            List<SubApplicationEntity> list = applicationService.list(appQuery);
            entity.setList(list);
        }
        exeByDate(entity);
    }

    /**
     * 构建查询时间和遍历条件
     * @param entity
     */
    public void exeByDate(ScheduleParamEntity entity){
        Date dayFirstDate = DateUtils.getDayFirstDate(new Date());
        if (entity.getStartDate() == null) {
            entity.setStartDate(DateUtils.getRollDate(dayFirstDate, -2));
        }
        if (entity.getStartDate() != null) {
            entity.setStartDate(DateUtils.getDayFirstDate(entity.getStartDate()));
        }
        if (entity.getEndDate() == null) {
            entity.setEndDate(dayFirstDate);
        }
        if (entity.getEndDate() != null) {
            entity.setEndDate(DateUtils.getDayFirstDate(entity.getEndDate()));
        }
        //设定为当日凌晨

        if (entity.getEndDate().compareTo(entity.getStartDate()) <= 0) {
            log.info("开始时间等于结束时间");
            return;
        }
        //记录停止日期
        Date endDate = entity.getEndDate();
        //每天执行
        entity.setEndDate(DateUtils.getRollDate(entity.getStartDate(), 1));

        while (endDate.compareTo(entity.getEndDate()) >= 0) {
            forEachCollect(entity);
            //开始时间后延1天
            entity.setStartDate(DateUtils.getRollDate(entity.getStartDate(), 1));
            //结束时间后延1天
            entity.setEndDate(DateUtils.getRollDate(entity.getEndDate(), 1));
        }
    }
    /**
     * 遍历应用处理数据
     *
     * @param entity
     * @return
     */
    public ScheduleParamEntity forEachCollect(ScheduleParamEntity entity) {

        Map<String, Object> map = new HashMap<>(4);
        map.putIfAbsent(START, entity.getStartDate());
        map.putIfAbsent(END,entity.getEndDate());
        //当天的数据分页处理
        List<SubApplicationEntity> list = entity.getList();
        if (ObjectUtils.isEmpty(list)) {
            return entity;
        }
        list.forEach(a -> exeApplicationSave(map, a, entity.getStartDate()));
        return entity;
    }

    /**
     * 需要实现具体的数据
     * @param map
     * @param entity
     * @param exeDate
     */
    abstract void exeApplicationSave(Map<String, Object> map, SubApplicationEntity entity, Date exeDate);
}
